package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.service.RoleService;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yzt
 * @create 2021-09-12 17:42
 */
@Controller
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    //显示页面角色信息
    @ResponseBody
    @RequestMapping("/role/get/page")
    public ResultEntity<PageInfo<Role>> getPage(
                        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                        @RequestParam(value = "keyword",defaultValue = "") String keyword){

        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);

        return ResultEntity.successWithData(pageInfo);
    }

    //保存角色
    @ResponseBody
    @RequestMapping("/role/save")
    public ResultEntity roleSave(Role role){
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }

    //更新角色信息
    @ResponseBody
    @RequestMapping("/role/update")
    public ResultEntity roleUpdate(Role role){
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    //根据id的数组进行批量删除
    @ResponseBody
    @RequestMapping("/role/remove/by/array")
    public ResultEntity roleDelete(@RequestBody List<Integer> roleIdList){
        roleService.RemoveRole(roleIdList);
        return ResultEntity.successWithoutData();
    }
}
